import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { powerDistributionFacilitiesState } from './typings'
import {
  getPowerDistributionFacilitiesList,
  exportPowerDistributionFacilitiesInfo,
  getPowerDistributionFacilitiesInfo,
  addPowerDistributionFacilitiesInfo,
  updatePowerDistributionFacilitiesInfo,
  updatePowerDistributionFacilitiesMapLocation,
  checkDeletedPowerDistributionFacilitiesInfo,
  deletePowerDistributionFacilitiesInfo,
  getDistributionSelectBoxList,
  savePowerDistributionFacilitiesLoop,
  getWhetherDeletePowerDistributionFacilitiesLoop,
  getAssociativeLoopInfoByDistributionId,
  importPowerDistributionFacilitiesInfo,
  tenantDictDictionaryGet
} from '~/api/assetManagement/facilityManagement/powerDistributionFacilities'
import { getDataDictionaryByCode } from '~/api/global'
import { filterValueEnum, DownloadAttachment } from '~/utils/util'

const powerDistributionFacilities: Module<
  powerDistributionFacilitiesState,
  powerDistributionFacilitiesState
> = {
  namespaced: true,
  state: {
    facilityTypeSelectList: [],
    facilityTypeEnum: {},
    operationStatusSelectList: [],
    operationStatusEnum: {},
    // operationStatusSelectList,
    // operationStatusEnum: filterValueEnum(operationStatusSelectList, 'key', 'value'),
    operationSelectList: [], // 投运状态 从数据字典维护
    operationEnum: {},
    tenantDictDictionaryOpt: []
  },
  getters: {
    facilityTypeSelectList: (state) => state.facilityTypeSelectList,
    facilityTypeEnum: (state) => state.facilityTypeEnum,
    operationStatusSelectList: (state) => state.operationStatusSelectList,
    operationStatusEnum: (state) => state.operationStatusEnum,
    tenantDictDictionaryOpt: (state) => state.tenantDictDictionaryOpt
  },
  mutations: {
    SET_FACILITY_TYPE_SELECT_LIST: (state, payload) => {
      state.facilityTypeSelectList = payload
      state.facilityTypeEnum = filterValueEnum(payload, 'id', 'title')
    },
    SET_TANANT_DICTIONARY: (state, payload) => {
      state.tenantDictDictionaryOpt = payload
    },
    // 投运状态 从数据字典维护
    SET_OPERATION_STATUS_SELECT_LIST: (state, payload) => {
      state.operationStatusSelectList = payload
      state.operationStatusEnum = filterValueEnum(payload, 'dictKey', 'dictValue')
    }
  },
  actions: {
    // 获取配电设施列表
    async GetPowerDistributionFacilitiesList<S, T>(_: ActionContext<S, T>, params: T) {
      return await getPowerDistributionFacilitiesList<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    // 导出除配电设施项
    async ExportPowerDistributionFacilitiesInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await exportPowerDistributionFacilitiesInfo<T>(params).then((res: AxiosResponse) => {
        const contentDisposition = res?.headers?.['content-disposition']
        if (contentDisposition && res.data) {
          DownloadAttachment(decodeURI(contentDisposition.substring(20)), res.data)
        }

        return res.data
      })
    },
    // 获取配电设施项基础资料
    async GetPowerDistributionFacilitiesInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await getPowerDistributionFacilitiesInfo<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    // 添加配电设施项基础资料
    async AddPowerDistributionFacilitiesInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await addPowerDistributionFacilitiesInfo<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    // 修改配电设施项基础资料
    async UpdatePowerDistributionFacilitiesInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await updatePowerDistributionFacilitiesInfo<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    // 修改配电设施项地图位置
    async UpdatePowerDistributionFacilitiesMapLocation<S, T>(_: ActionContext<S, T>, params: T) {
      return await updatePowerDistributionFacilitiesMapLocation<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    // 校验配电设施是否可删除
    async CheckDeletedPowerDistributionFacilitiesInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await checkDeletedPowerDistributionFacilitiesInfo<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    // 删除配电设施项
    async DeletePowerDistributionFacilitiesInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await deletePowerDistributionFacilitiesInfo<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    // 获取设备类别下拉列表
    async GetDistributiontTypeSelectList<S, T>({ commit }: ActionContext<S, T>) {
      // parentCode=Distribution
      return await getDataDictionaryByCode({ code: 'distribution_type' }).then(
        (res: AxiosResponse) => {
          commit('SET_FACILITY_TYPE_SELECT_LIST', res?.data?.data || [])
          return res?.data
        }
      )
    },
    // 配电设备类型
    async TenantDictDictionaryGet<S, T>({ commit }: ActionContext<S, T>) {
      // parentCode=Distribution
      return await tenantDictDictionaryGet({ code: 'distribution_type' }).then(
        (res: AxiosResponse) => {
          commit('SET_TANANT_DICTIONARY', res?.data?.data || [])
          return res?.data
        }
      )
    },
    // 获取投运状态 数据字典
    async GetOperationStatusSelectList<S, T>({ commit }: ActionContext<S, T>) {
      return await getDataDictionaryByCode({ code: 'is_deliver' }).then((res: AxiosResponse) => {
        commit('SET_OPERATION_STATUS_SELECT_LIST', res?.data?.data || [])
        return res?.data
      })
    },
    // 所属配电设施 下拉列表
    async GetDistributionSelectBoxList<S, T>(_: ActionContext<S, T>, params: T) {
      return await getDistributionSelectBoxList<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 保存配电设施回路
    async SavePowerDistributionFacilitiesLoop<S, T>(_: ActionContext<S, T>, params: T) {
      return await savePowerDistributionFacilitiesLoop<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    // 查询配电设施是否可删除
    async GetWhetherDeletePowerDistributionFacilitiesLoop<S, T>(_: ActionContext<S, T>, params: T) {
      return await getWhetherDeletePowerDistributionFacilitiesLoop<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    // 根据配电设施id查询关联回路及回路关联灯杆和灯的信息
    async GetAssociativeLoopInfoByDistributionId<S, T>(_: ActionContext<S, T>, params: T) {
      return await getAssociativeLoopInfoByDistributionId<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    },
    // 配电设施导入文件
    async ImportPowerDistributionFacilitiesInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await importPowerDistributionFacilitiesInfo<T>(params).then(
        (res: AxiosResponse) => res?.data
      )
    }
  }
}

export default powerDistributionFacilities
